/* Author: Mark Mckillion, Chris Mowbray
 * Date: 17/04/2012
 * Version 1.0.*/

/* This is the the Bubble Sorting method that works by repeatedly stepping through
 * the list to be sorted, comparing each pair of adjacent items and swapping them 
 * if they are in the wrong order
 
 */


package assignment4;

import java.awt.Color;
import java.awt.Image;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;

public class BubbleSort {

    // as mentioned other classes we get the current text from the textpane and
    // create a styled document so be edited.
    static StyledDocument sdoc = ModelFrame.algoritm.getStyledDocument();
    static SimpleAttributeSet attrs = new SimpleAttributeSet();
    // get the paragraph of the textpane
    static Element section = ModelFrame.algoritm.getDocument().getDefaultRootElement();
    // get the number of lines
    int paraCount = section.getElementCount();
    // int that i sused to sleep the current thread, updated by the slider
    public static int speed;

    
    
    // the bubble sort algorithm with 'Thread.sleep' throughout so that this sorting
    // algoritm will be slowed down.
    // for the colurs
    public static void sortObjects(Color [] list) {

        list = Colour.colors;
        int position, scan;
        Color temp;
        // we highlight line four
        highlight(4);
        // sleep for specified time
        try {
            Thread.sleep(speed);
        } catch (Exception e) {
        }
        //then dehighlight the line.
        deHighlight(4);
        
        for (position = list.length - 1; position >= 0; position--) {
     
            highlight(6);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(6);
            
            for (scan = 0; scan <= position - 1; scan++) 
            {   highlight(8);
                try 
                {   Thread.sleep(speed);
                }   catch (Exception e)
                    {
                    }
                deHighlight(8);

                DrawingPanel.comp1 = scan;
                DrawingPanel.comp2 = scan + 1;
                ModelFrame.drawing.repaint();
                
                if (ControlPanel.jComboBox3.getSelectedItem().equals("Colour"))
                {   if (Colour.colour.compareTo(list[scan], list[scan + 1]) == 1) 
                    {   temp = list[scan + 1];

                    highlight(10);
                        try 
                        {   Thread.sleep(speed);
                        }   catch (Exception e) 
                            {
                            }
                    deHighlight(10);

                    list[scan + 1] = list[scan];
                    highlight(11);

                    try 
                    {   Thread.sleep(speed);
                    }   catch (Exception e) 
                        {
                        }
                    deHighlight(11);

                    list[scan] = temp;

                    highlight(12);
                    try 
                    {   Thread.sleep(speed);
                    }   catch (Exception e) 
                        {
                        }
                    deHighlight(12);

                    ModelFrame.drawing.repaint();
                    }
                }
            }   
            DrawingPanel.sn++;
            ModelFrame.drawing.repaint();
        }
        highlight(16);
    }
    
    // sorthing algoritm for the vehicles
        public static void sortObjects(Image [] list) {

        list = Vehicle.vehicles;
        int position, scan;
        Image temp;

        highlight(4);
        try {
            Thread.sleep(speed);
        } catch (Exception e) {
        }
        deHighlight(4);
        
        for (position = list.length - 1; position >= 0; position--) {
     
            highlight(6);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(6);
            
            for (scan = 0; scan <= position - 1; scan++) 
            {   highlight(8);
                try 
                {   Thread.sleep(speed);
                }   catch (Exception e)
                    {
                    }
                deHighlight(8);

                DrawingPanel.comp1 = scan;
                DrawingPanel.comp2 = scan + 1;
                ModelFrame.drawing.repaint();
                
                if (ControlPanel.jComboBox3.getSelectedItem().equals("Vehicle"))
                {   if (Vehicle.vehicle.compareTo(list[scan], list[scan + 1]) == 1) 
                    {   temp = list[scan + 1];

                    highlight(10);
                        try 
                        {   Thread.sleep(speed);
                        }   catch (Exception e) 
                            {
                            }
                    deHighlight(10);

                    list[scan + 1] = list[scan];
                    highlight(11);

                    try 
                    {   Thread.sleep(speed);
                    }   catch (Exception e) 
                        {
                        }
                    deHighlight(11);

                    list[scan] = temp;

                    highlight(12);
                    try 
                    {   Thread.sleep(speed);
                    }   catch (Exception e) 
                        {
                        }
                    deHighlight(12);

                    ModelFrame.drawing.repaint();
                    }
                }
            }   
            DrawingPanel.sn++;
            ModelFrame.drawing.repaint();
        }
        highlight(16);
    }
    
        
        // this is a method to highlight the current line that the thread is currently on
        // so that the user can follow what is happening.
    public static void highlight(int i) {
        
        // setting the current line as an element
        Element e = section.getElement(i);
        // getting start of the line.
        int rangeStart = e.getStartOffset();
        // getting end of the line
        int rangeEnd = e.getEndOffset();
        
        //these 3 lines together slect the line from start to end and highligh using
        // the colour gray.
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
        StyleConstants.setBackground(attrs, Color.LIGHT_GRAY); 
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
     
    }

    // the deHighlight method is initially the same as the highlight method but it changes
    // the background to white to give the illusion of it being highlighted.
    public static void deHighlight(int i) {
        Element e = section.getElement(i);
        int rangeStart = e.getStartOffset();
        int rangeEnd = e.getEndOffset();

        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
        StyleConstants.setBackground(attrs, Color.WHITE); // change to white
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
   
    }
    
    // method that is called in the jsldier actionlistner to get the current speed
    // of the threads by way to sleeping the thread.
    public static void getV(int v)
    {
        int i = v * 75;
        speed = 800 - i;
    }
}   //End of BubbleSort class.